package com.huixue.cn.ztej.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginFilter implements Filter{


	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {
		if (!(req instanceof HttpServletRequest)
				|| !(res instanceof HttpServletResponse)) {
			throw new ServletException("此过滤器只保护HTTP资源");
		}
		HttpServletRequest request = (HttpServletRequest)req;
		HttpServletResponse response = (HttpServletResponse)res;
		String url = request.getRequestURI();
		if(url.toLowerCase().contains("login") || url.toLowerCase().equals("/ZTERWeChat/")){
			chain.doFilter(req, res);
			return;
		}
		else if(url.toLowerCase().contains("/sys/")){
				if(request.getSession().getAttribute("loginAccount")==null || url.toLowerCase().contains("login")){
					response.sendRedirect(request.getContextPath());
					return;
				}
			}
		chain.doFilter(req, res);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

	
	
}
